//
//  UIButton+Addition.m
//  Project
//
//  Created by  CarlsonLee on 2018/12/14.
//  Copyright © 2018年 YANJING. All rights reserved.
//

#import "UIButton+Addition.h"
#import <objc/runtime.h>

@interface UIButton ()

@property (nonatomic, assign) NSTimeInterval gel_acceptEventTime;

@end

@implementation UIButton (Addition)

+ (void)load {
    [super load];
    [self gel_systemSelector:@selector(sendAction:to:forEvent:) swizzledSelector:@selector(gel_sendAction:to:forEvent:) error:nil];
}

- (void)gel_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event
{
    BOOL needSendAction = (NSDate.date.timeIntervalSince1970 - self.gel_acceptEventTime >= self.gel_eventInterval);
    
    if (self.gel_eventInterval > 0) {
        self.gel_acceptEventTime = NSDate.date.timeIntervalSince1970;
    }
    
    if (needSendAction) {
        [self gel_sendAction:action to:target forEvent:event];
    }
}

#pragma mark - Getter

- (NSTimeInterval)gel_eventInterval
{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventInterval") doubleValue];
}

- (void)setGel_eventInterval:(NSTimeInterval)gel_eventInterval
{
    objc_setAssociatedObject(self, "UIControl_acceptEventInterval", @(gel_eventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSTimeInterval)gel_acceptEventTime
{
    return [objc_getAssociatedObject(self, "UIControl_acceptEventTime") doubleValue];
}

- (void)setGel_acceptEventTime:(NSTimeInterval)gel_acceptEventTime
{
    objc_setAssociatedObject(self, "UIControl_acceptEventTime", @(gel_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end
